//
//  UIImage+Converter.m
//  SLEngine
//
//  Created by Victor on 15/6/3.
//  Copyright (c) 2015年 Sunline. All rights reserved.
//

#import "UIImage+Converter.h"

@implementation UIImage (Converter)


+ (UIImage *)suitableImageWithName:(NSString *)imageName {
    if (![imageName length]) {
        return nil;
    }
    
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    CGFloat screenWidth = CGRectGetWidth(screenBounds);
    CGFloat screenHeight = CGRectGetHeight(screenBounds);

    NSString *suitableImageName = nil;
    if (screenWidth == 320 && screenHeight == 480) {
        suitableImageName = [imageName stringByAppendingString:@"@2x.jpg"];
    }else if (screenWidth == 320 && screenHeight == 568) {
        suitableImageName = [imageName stringByAppendingString:@"-568h@2x.jpg"];
    }else if (screenWidth == 375 && screenHeight == 667) {
        suitableImageName = [imageName stringByAppendingString:@"-667h@2x.jpg"];
    }else if (screenWidth == 414 && screenHeight == 736) {
        suitableImageName = [imageName stringByAppendingString:@"-736h@3x.jpg"];
    }else if (screenWidth == 375 && screenHeight == 812){
        suitableImageName = [imageName stringByAppendingString:@"-812h@3x.jpg"];
    }
    
    if ([suitableImageName length]) {
        return [UIImage imageNamed:suitableImageName];
    }else {
        return nil;
    }
}

+ (UIImage *)suitablePNGImageWithName:(NSString *)imageName {
    if (![imageName length]) {
        return nil;
    }
    
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    CGFloat screenWidth = CGRectGetWidth(screenBounds);
    CGFloat screenHeight = CGRectGetHeight(screenBounds);
    
    NSString *suitableImageName = nil;
    if (screenWidth == 320 && screenHeight == 480) {
        suitableImageName = [imageName stringByAppendingString:@"@2x.png"];
    }else if (screenWidth == 320 && screenHeight == 568) {
        suitableImageName = [imageName stringByAppendingString:@"-568h@2x.png"];
    }else if (screenWidth == 375 && screenHeight == 667) {
        suitableImageName = [imageName stringByAppendingString:@"-667h@2x.png"];
    }else if (screenWidth == 414 && screenHeight == 736) {
        suitableImageName = [imageName stringByAppendingString:@"-736h@3x.png"];
    }else if (screenWidth == 375 && screenHeight == 812){
        suitableImageName = [imageName stringByAppendingString:@"-812h@3x.png"];
    }
    
    if ([suitableImageName length]) {
        return [UIImage imageNamed:suitableImageName];
    }else {
        return nil;
    }
}
+ (UIImage *)imageWithColor:(UIColor *)color {
    return [self imageWithColor:color size:CGSizeMake(1.0f, 1.0f)];
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, 0, [UIScreen mainScreen].scale);
    [color set];
    UIRectFill(CGRectMake(0, 0, size.width, size.height));
    UIImage *pureColorImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return pureColorImage;
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size circle:(BOOL)circle{
     UIImage * image = [self imageWithColor:color size:size];
    if(circle){
        UIImage *image2 = [self imageWithIconImage:image borderImage:[self imageWithColor:[UIColor clearColor] size:size] border:0];
       return [self imageResize:image2 andResizeTo:CGSizeMake(size.width/2, size.height/2)];
    }else{
        UIImage * image = [self imageWithColor:color size:size];
        return image;
    }  
}

+(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize
{
    CGFloat scale = [[UIScreen mainScreen]scale];
    UIGraphicsBeginImageContextWithOptions(newSize, NO, scale);
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
+ (UIImage*)imageWithIconName:(NSString *)icon borderImage:(NSString *)borderImage border:(int)border{
    //头像图片
    UIImage * image = [UIImage imageNamed:icon];
    //边框图片
    UIImage * borderImg = [UIImage imageNamed:borderImage];
    return [self imageWithIconImage:image borderImage:borderImg border:border];
}

+ (UIImage*)imageWithIconImage:(UIImage *)icon borderImage:(UIImage *)borderImage border:(int)border{
    //头像图片
    UIImage * image = icon;
    //边框图片
    UIImage * borderImg = borderImage;
    //
    CGSize size = CGSizeMake(image.size.width + border, image.size.height + border);
    
    //创建图片上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    //绘制边框的圆
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, size.width, size.height));
    
    //剪切可视范围
    CGContextClip(context);
    
    //绘制边框图片
    [borderImg drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    //设置头像frame
    CGFloat iconX = border / 2;
    CGFloat iconY = border / 2;
    CGFloat iconW = image.size.width;
    CGFloat iconH = image.size.height;
    
    //绘制圆形头像范围
    CGContextAddEllipseInRect(context, CGRectMake(iconX, iconY, iconW, iconH));
    
    //剪切可视范围
    CGContextClip(context);
    
    //绘制头像
    [image drawInRect:CGRectMake(iconX, iconY, iconW, iconH)];
    
    //取出整个图片上下文的图片
    UIImage *iconImage = UIGraphicsGetImageFromCurrentImageContext();
    
    return iconImage;
}

-(nonnull UIImage*)modifyImageSize:(CGSize)size
{
    if(CGSizeEqualToSize(self.size, size))
    {
        return self;
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

// Base64图片 -> UIImage

+ (UIImage *) dataURL2Image: (NSString *) imgSrc
{
    NSURL *url = [NSURL URLWithString: imgSrc];
    NSData *data = [NSData dataWithContentsOfURL: url];
    UIImage *image = [UIImage imageWithData: data];
    
    return image;
}


// UIImage -> Base64图片

+ (BOOL) imageHasAlpha: (UIImage *) image
{
    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
    return (alpha == kCGImageAlphaFirst ||
            alpha == kCGImageAlphaLast ||
            alpha == kCGImageAlphaPremultipliedFirst ||
            alpha == kCGImageAlphaPremultipliedLast);
}

+ (NSString *) image2DataURL: (UIImage *) image
{
    NSData *imageData = nil;
    NSString *mimeType = nil;
    
    if ([self imageHasAlpha: image]) {
        imageData = UIImagePNGRepresentation(image);
        mimeType = @"image/png";
    } else {
        imageData = UIImageJPEGRepresentation(image, 1.0f);
        mimeType = @"image/jpeg";
    }
    
    return [NSString stringWithFormat:@"data:%@;base64,%@", mimeType,
            [imageData base64EncodedStringWithOptions: 0]];
    
}

@end
